package ywpt.service.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ClientDevice;
import ywpt.model.ClientInfo;
import ywpt.model.CommonTable;
import ywpt.model.JobInfo;
import ywpt.model.PeopleInfo;
import ywpt.model.RepairInfo;
import ywpt.model.SysMsgInfo;
import ywpt.service.work.SysMsgInfoService;
import ywpt.web.common.PublicFunction;
@Service("repairInfoService")
@RemoteProxy(name="repairInfoService")
public class RepairInfoServiceImpl extends HibernateEntityDao<RepairInfo> implements RepairInfoService{

	private SysMsgInfoService sysMsgInfoService;
	
	public SysMsgInfoService getSysMsgInfoService() {
		return sysMsgInfoService;
	}

	public void setSysMsgInfoService(SysMsgInfoService sysMsgInfoService) {
		this.sysMsgInfoService = sysMsgInfoService;
	}
	
	@Override
	public String ceratRepairCode() {
		SimpleDateFormat FORMAT = new SimpleDateFormat("yyyyMMdd");
		DetachedCriteria query = DetachedCriteria.forClass(RepairInfo.class);
		query.add(Restrictions.like("code",FORMAT.format(new Date())+"%"));
		String repairCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		query.setProjection(null);
		List<RepairInfo> repairInfos = this.getRepairInfos(query);
		if ( repairInfos.iterator().hasNext() )	
		{
			code = (repairInfos.iterator().next().getCode()).substring(8);
			maxId = new Integer(code);
		}
		repairCode = PublicFunction.getNextCode("", maxId, false,3); 
		
		return FORMAT.format(new Date())+repairCode;
	}
	
	@Override
	public int getCallRecordCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@Override
	@RemoteMethod
	public RepairInfo getRepairInfoById(Long id) {
		return this.get(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RepairInfo> getRepairInfos(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public void saveRepairInfo(RepairInfo repairInfo, Long jobId,
			Long clientId,Long deviceId,Long getUserId, Long sendUserId, Long responseId,
			Long typeId, Long resultId) {
		if(jobId!=null)
			repairInfo.setJob(this.get(JobInfo.class, jobId));
		if(clientId!=null)
			repairInfo.setClient(this.get(ClientInfo.class, clientId));
		if(deviceId!=null)
			repairInfo.setDevice(this.get(ClientDevice.class,deviceId));
		if(sendUserId!=null)
			repairInfo.setSendUser(this.get(PeopleInfo.class, sendUserId));
		if(getUserId != null)
			repairInfo.setGetUser(this.get(PeopleInfo.class, getUserId));
		if(responseId!=null){
			PeopleInfo peopleInfo= this.get(PeopleInfo.class, responseId);
			repairInfo.setResponseUser(peopleInfo);
			repairInfo.setResponseName(peopleInfo.getPeopleName());
		}
		if(typeId!=null)
			repairInfo.setRepareType(this.get(CommonTable.class, typeId));
		if(resultId!=null)
			repairInfo.setRepareResult(this.get(CommonTable.class, resultId));
		this.save(repairInfo);
		if (responseId!=null) {
			sysMsgInfoService.invalidMsg(3, repairInfo.getId());
			addSysMsg( repairInfo ,responseId);
		}
	}

	@Override
	public void updateRepairInfo(RepairInfo repairInfo, Long jobId,
			Long clientId,Long deviceId,Long getUserId, Long sendUserId, Long responseId,
			Long typeId, Long resultId) {
		if(jobId!=null)
			repairInfo.setJob(this.get(JobInfo.class, jobId));
		if(clientId!=null)
			repairInfo.setClient(this.get(ClientInfo.class, clientId));
		if(deviceId!=null){
			repairInfo.setDevice(this.get(ClientDevice.class,deviceId));	
		}
		if(sendUserId!=null)
			repairInfo.setSendUser(this.get(PeopleInfo.class, sendUserId));
		if(getUserId != null)
			repairInfo.setGetUser(this.get(PeopleInfo.class, getUserId));
		if(responseId!=null){
			PeopleInfo peopleInfo= this.get(PeopleInfo.class, responseId);
			repairInfo.setResponseUser(peopleInfo);
			repairInfo.setResponseName(peopleInfo.getPeopleName());
		}
		if(typeId!=null)
			repairInfo.setRepareType(this.get(CommonTable.class, typeId));
		if(resultId!=null)
			repairInfo.setRepareResult(this.get(CommonTable.class, resultId));
		super.update(repairInfo);
		/*if (responseId!=null) {
			sysMsgInfoService.invalidMsg(3, repairInfo.getId());
			addSysMsg( repairInfo ,responseId);
		}*/
	}

	@Override
	public void removeRepairInfo(Long id) {
		RepairInfo repairInfo = this.get(id);
		repairInfo.setDeleteFlag(true);
		this.update(repairInfo);
	}

	@Override
	public void checkRepairInfo(Long id) {
		RepairInfo repairInfo = this.get(id);
		repairInfo.setCheckFlag(true);
		this.update(repairInfo);		
	}

	@Override
	public void disCheckRepairInfo(Long id) {
		RepairInfo repairInfo = this.get(id);
		repairInfo.setCheckFlag(false);
		this.update(repairInfo);		
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<RepairInfo> getRepairInfosByQstr(String qstr, int start,
			int size) {
		DetachedCriteria query = DetachedCriteria.forClass(RepairInfo.class);
		query.add(Restrictions.eq("deleteFlag", false));
		if ( qstr != null && !qstr.trim().equals("") )
		{
			query.createAlias("client", "client");
			query.createAlias("device", "device");
			query.add(Restrictions.or(Restrictions.like("client.clientName", "%" + qstr + "%"),  
							          Restrictions.like("device.name", "%" + qstr + "%")));
				// .add(Restrictions.eq("organType", "01"));
			
		}
		query.addOrder(Order.asc("id"));
		List<RepairInfo> repairInfoList = new ArrayList<RepairInfo>();
		List<RepairInfo> repairInfoLists = new ArrayList<RepairInfo>();
		RepairInfo repairInfo =new RepairInfo();
		
		repairInfoList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<RepairInfo> itr = repairInfoList.iterator(); itr.hasNext(); i++ ) 
		{
 			repairInfo = itr.next();
 			System.out.println(repairInfo.getStr());
			String ss = PublicFunction.format(repairInfo.getClient().getClientName() + "",7, "  ", 1)
			          + repairInfo.getDevice().getName(); 
			//repairInfo.setClientDevice(ss);
			System.out.println(ss);
			repairInfoLists.add(repairInfo);
		}
		return repairInfoLists;
	}

	@Override
	@RemoteMethod
	public int getRepairInfosCountByQstr(String qstr) {
		DetachedCriteria query = DetachedCriteria.forClass(RepairInfo.class);
		query.add(Restrictions.eq("deleteFlag", false));
		if ( qstr != null && !qstr.trim().equals("") )
		{
			query.createAlias("client", "client");
			query.createAlias("device", "device");
			query.add(Restrictions.or(Restrictions.like("client.clientName", "%" + qstr + "%"),  
							          Restrictions.like("device.name", "%" + qstr + "%")));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}

	protected void addSysMsg(RepairInfo repairInfo ,Long reveId) {
		SysMsgInfo sysMsgInfo = new SysMsgInfo();
		sysMsgInfo.setMsgTitle("维修信息");
		sysMsgInfo.setMsgContent(repairInfo.getCreater().getPeopleName()+"向你提交一条维修信息！");
		sysMsgInfo.setReceiveUser(reveId +",");
		sysMsgInfo.setCreateDate(new Date());
		sysMsgInfo.setBusinessType(3);
		sysMsgInfo.setBusinessId(repairInfo.getId());
		sysMsgInfo.setUrl("/device/repairInfo/edit-"+repairInfo.getId()+".action");
		sysMsgInfo.setCreateDate(new Date());
		sysMsgInfoService.saveSysMsgInfo(sysMsgInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<RepairInfo> getRepairInfosByDevice(Long deviceId) {
		DetachedCriteria query = DetachedCriteria.forClass(RepairInfo.class);
		if (deviceId!=null) {
			query.createAlias("device", "device").add(Restrictions.eq("device.id", deviceId));
		}
		query.add(Restrictions.eq("deleteFlag", false));
		return this.findAllByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RepairInfo> getRepairInfos(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	@RemoteMethod
	public List<RepairInfo> getRepairInfosByCode(String repairCode, int start,int size) {
		DetachedCriteria query = DetachedCriteria.forClass(RepairInfo.class);
		query.add(Restrictions.eq("deleteFlag", false));
		query.add(Restrictions.isNotNull("code"));
		if ( repairCode != null && !repairCode.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("code", "%" + repairCode + "%"),  
							          Restrictions.like("code", "%" + repairCode + "%")));
				// .add(Restrictions.eq("organType", "01"));
		}
		query.addOrder(Order.desc("id"));
		query.setProjection(null);
		List<RepairInfo> repairList = new ArrayList<RepairInfo>();
		List<RepairInfo> repairLists = new ArrayList<RepairInfo>();
		RepairInfo repairInfo =new RepairInfo();
		
		repairList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<RepairInfo> itr = repairList.iterator(); itr.hasNext(); i++ ) 
		{
			repairInfo = itr.next();
			String ss = PublicFunction.format(repairInfo.getCode() + "",7, "  ", 1)
			          + ""; 
			
			repairInfo.setCode(ss);
			repairLists.add(repairInfo);
		}
		return repairLists;
	}

	@Override
	@RemoteMethod
	public int getRepairInfosCountByCode(String repairCode) {
		DetachedCriteria query = DetachedCriteria.forClass(RepairInfo.class);
		query.add(Restrictions.eq("deleteFlag", false));
		query.add(Restrictions.isNotNull("code"));
		if ( repairCode != null && !repairCode.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("code", "%" + repairCode + "%" ),  
							          Restrictions.like("code", "%" + repairCode + "%" )));
				 //.add(Restrictions.eq("organType", "01"));
		}
		//query.addOrder(Order.asc("jobCode"));
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}

}
